//
//  BIDCommunityDedicationViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/23.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDCommunityDedicationViewController.h"
#import "BIDCommunityDedicationDetailViewController.h"
#import "BIDCommunityDedicationCell.h"
#import "NSDate+Category.h"

static NSString *communityDedicationListURL = @"socialPublicAndroidInfo/list.action";

@interface BIDCommunityDedicationViewController ()<BIDBaseListViewDelegate>
{
    BOOL _bRegister;
}
@end

@implementation BIDCommunityDedicationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView.myDelegate = self;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    parent.title = @"社区公益";
    parent.navigationController.navigationBarHidden = NO;
    parent.navigationItem.rightBarButtonItem = nil;
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], communityDedicationListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld", self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
        [self autoDecideToShowEmptyView];
    }];
}

- (NSString *)transformDate:(NSString *)stringDate
{
    NSString *res = @"";
    NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *originalDate = [dateFormatter dateFromString:stringDate];
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter1 = [NSDateFormatter dateFormatterWithFormat:@"HH:mm"];
    NSString *time = [dateFormatter1 stringFromDate:originalDate];
    if([originalDate isToday])
    {
        res = time;
    }
    else if([originalDate isYesterday])
    {
        res = [[NSString alloc] initWithFormat:@"昨天 %@", time];
    }
    else
    {
        if([originalDate year] == [today year])
        {
            NSDateFormatter *dateFormatter2 = [NSDateFormatter dateFormatterWithFormat:@"MM-dd HH:mm"];
            res = [dateFormatter2 stringFromDate:originalDate];
        }
        else
        {
            NSDateFormatter *dateFormatter3 = [NSDateFormatter dateFormatterWithFormat:@"yyyy-MM-dd HH:mm"];
            res = [dateFormatter3 stringFromDate:originalDate];
        }
    }
    return res;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDCommunityDedicationCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    BIDCommunityDedicationCell *communityDedicationCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSString *strType = dic[@"type"];
    NSString *strDate = [self transformDate:dic[@"createTime"]];
    NSString *strTitle = [[NSString alloc] initWithFormat:@"%@ %@", strType, strDate];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:strTitle];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(strType.length+1, strDate.length)];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.f] range:NSMakeRange(strType.length+1, strDate.length)];
    communityDedicationCell.titleLabel.attributedText = attributeStr;
    communityDedicationCell.contentLabel.text = dic[@"content"];
    cell = communityDedicationCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = indexPath.row;
    BIDCommunityDedicationDetailViewController *vc = [[BIDCommunityDedicationDetailViewController alloc] initWithNibName:@"BIDCommunityDedicationDetailViewController" bundle:nil];
    vc.title = self.dataSourceArr[row][@"type"];
    vc.communityDedicationID = self.dataSourceArr[row][@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 89.f;
}

@end
